package com.open.capacity.user.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Dictionary;
import com.open.capacity.common.model.SysUser;
import com.open.capacity.common.model.TitleType;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.log.annotation.LogAnnotation;
import com.open.capacity.user.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@Api(tags = "Dictionary API")
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 字典查询
     * @param params
     * @return
     * @throws ControllerException
     */
//    @PreAuthorize("hasAuthority('user:get/users')")
    @ApiOperation(value = "查询字典列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @PostMapping("/getSelectDictionaries")
    @LogAnnotation(module = "user-center", recordRequestParam = false)
    public PageResult<Dictionary> findUsers(@RequestParam Map<String, Object> params) throws ControllerException {
        try {
            return dictionaryService.getSelectDictionaries(params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    // 字典添加
    @PostMapping("/addDictionary")
    @ApiOperation(value = "字典添加")
    @LogAnnotation(module = "user-center", recordRequestParam = false)
    public Result addTitleType(@RequestBody Dictionary dictionary){
        return dictionaryService.addDictionary(dictionary);
    }

    /**
     * 修改字典状态
     *
     * @param params
     * @return
     * @throws ControllerException
     * @author gitgeek
     */
    @ApiOperation(value = "修改字典状态")
    @GetMapping("/updateDictionaryEnabled")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictid", value = "字典id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "enabled", value = "是否启用", required = true, dataType = "Boolean")
    })
    @LogAnnotation(module = "user-center", recordRequestParam = false)
//    @PreAuthorize("hasAnyAuthority('user:get/users/updateEnabled' ,'user:put/users/me')")
    public Result updateDictionaryEnabled(@RequestParam Map<String, Object> params) throws ControllerException {
        try {
            Long id = MapUtils.getLong(params, "id");
            if (id == 1277137734524300032L) {
                return Result.failed("超级管理员不给予修改");
            }
            return dictionaryService.updateDictionaryEnabled(params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    // 修改字典类型
    @PostMapping("/updateDictionary")
    @ApiOperation(value = "修改字典类型")
    public Result updateTitleType(@RequestBody Dictionary dictionary){
        return dictionaryService.updateDictionary(dictionary);
    }

}
